﻿using EmployeeLeaveSystem.Shared.DAO;
using EmployeeLeaveSystem.Shared.Models;
using Microsoft.AspNetCore.Mvc;

namespace EmployeeLeaveSystem.Server.Controllers {
    [ApiController]
    [Route("[Controller]/[Action]")]
    public class TestController : ControllerBase {
        readonly EpMainContext _context;
        public TestController(EpMainContext context) {
            _context = context;
        }

        [HttpGet]
        public NetworkData Test() {
            var res = new NetworkData();
            res.errMsg = "test";

            LeaveDetail msg = new();
            msg.Name = "事假，需要回家玩游戏";
            msg.Description = "需要请假回家一天，用于玩游戏和玩电脑";
            msg.StartTime = DateTime.Now;
            msg.EndTime = new DateTime(2023, 5, 1);
            msg.StaffId = 5;
            msg.AcceptStaffId = 4;
            _context.Add(msg);
            _context.SaveChanges();

            return res;
        }
        /// <summary>
        /// 将初始化的数据加入数据库
        /// </summary>
        public NetworkData InitDataBase() {
            var res = new NetworkData();
            try {
                // 创建部门信息
                Department d1 = new(), d2 = new(), d3 = new();
                d1.Name = "信息工程学院";
                d1.Description = "丹阳校区信息工程学院";
                d2.Name = "人文学院";
                d2.Description = "春日PU活动正在来袭，三行情诗火热进行中！";
                d3.Name = "教师职工部";
                d3.Description = "这里是教师职工部门，用于教师职工";
                _context.AddRange(d1, d2, d3);
                // 人员信息
                StaffInfo s1 = new(), s2 = new(), s3 = new(), s4 = new();
                s1.Name = "方老师";
                s1.Department = d3;
                s1.Type = StaffType.Manager;
                s2.Name = "小熊";
                s2.Description = "82200620学生";
                s2.Department = d1;
                s2.UpperStaff = s1;
                s3.Name = "小泉";
                s3.Department = d2;
                s3.UpperStaff = s1;
                s4.Name = "小龚";
                s4.Department = d1;
                s4.UpperStaff = s3;
                _context.AddRange(s1, s2, s3, s4);
                // 新增一条请假信息信息
                LeaveDetail msg = new();
                msg.Name = "事假，需要回家玩游戏";
                msg.Description = "需要请假回家一天，用于玩游戏和玩电脑";
                msg.StartTime = DateTime.Now;
                msg.EndTime = new DateTime(2023, 5, 1);
                msg.Staff = s2;
                msg.AcceptStaff = s1;
                _context.Add(msg);
                _context.SaveChanges();
                // err码
                res.errMsg = "成功加入到了数据库之中";
                res.errCode = 200;
            }
            catch (Exception ex) {
                Console.WriteLine("错误：" + ex.Message, ConsoleColor.Red);
                res.errMsg = ex.Message;
                res.errCode = 502;
            }

            return res;
        }
    }
}
